home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / text / edit / vim60src.lha / Vim / vim60 / src / mark.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-26  |  32.8 KB  |  1,376 lines

  1. /* vi:set ts=8 sts=4 sw=4:
  2.  *
  3.  * VIM - Vi IMproved    by Bram Moolenaar
  4.  *
  5.  * Do ":help uganda"  in Vim to read copying and usage conditions.
  6.  * Do ":help credits" in Vim to see a list of people who contributed.
  7.  * See README.txt for an overview of the Vim source code.
  8.  */
  9.  
  10. /*
  11.  * mark.c: functions for setting marks and jumping to them
  12.  */
  13.  
  14. #include "vim.h"
  15.  
  16. /*
  17.  * This file contains routines to maintain and manipulate marks.
  18.  */
  19.  
  20. /*
  21.  * If a named file mark's lnum is non-zero, it is valid.
  22.  * If a named file mark's fnum is non-zero, it is for an existing buffer,
  23.  * otherwise it is from .viminfo and namedfm[n].fname is the file name.
  24.  * There are marks 'A - 'Z (set by user) and '0 to '9 (set when writing
  25.  * viminfo).
  26.  */
  27. #define EXTRA_MARKS 10                    /* marks 0-9 */
  28. static xfmark_T namedfm[NMARKS + EXTRA_MARKS];        /* marks with file nr */
  29.  
  30. static void fname2fnum __ARGS((xfmark_T *fm));
  31. static void fmarks_check_one __ARGS((xfmark_T *fm, char_u *name, buf_T *buf));
  32. static char_u *mark_line __ARGS((pos_T *mp, int lead_len));
  33. static void show_one_mark __ARGS((int, char_u *, pos_T *, char_u *, int current));
  34. #ifdef FEAT_JUMPLIST
  35. static void cleanup_jumplist __ARGS((void));
  36. #endif
  37. #ifdef FEAT_VIMINFO
  38. static void write_one_filemark __ARGS((FILE *fp, xfmark_T *fm, int c1, int c2));
  39. #endif
  40.  
  41. /*
  42.  * Set named mark 'c' at current cursor position.
  43.  * Returns OK on success, FAIL if bad name given.
  44.  */
  45.     int
  46. setmark(c)
  47.     int        c;
  48. {
  49.     int        i;
  50.  
  51.     /* Check for a special key (may cause islower() to crash). */
  52.     if (c < 0)
  53.     return FAIL;
  54.  
  55.     if (c == '\'' || c == '`')
  56.     {
  57.     setpcmark();
  58.     /* keep it even when the cursor doesn't move */
  59.     curwin->w_prev_pcmark = curwin->w_pcmark;
  60.     return OK;
  61.     }
  62.  
  63. #ifndef EBCDIC
  64.     if (c > 'z')        /* some islower() and isupper() cannot handle
  65.                 characters above 127 */
  66.     return FAIL;
  67. #endif
  68.     if (islower(c))
  69.     {
  70.     i = c - 'a';
  71.     curbuf->b_namedm[i] = curwin->w_cursor;
  72.     return OK;
  73.     }
  74.     if (isupper(c))
  75.     {
  76.     i = c - 'A';
  77.     namedfm[i].fmark.mark = curwin->w_cursor;
  78.     namedfm[i].fmark.fnum = curbuf->b_fnum;
  79.     vim_free(namedfm[i].fname);
  80.     namedfm[i].fname = NULL;
  81.     return OK;
  82.     }
  83.     return FAIL;
  84. }
  85.  
  86. /*
  87.  * Set the previous context mark to the current position and add it to the
  88.  * jump list.
  89.  */
  90.     void
  91. setpcmark()
  92. {
  93. #ifdef FEAT_JUMPLIST
  94.     int        i;
  95.     xfmark_T    *fm;
  96. #endif
  97. #ifdef JUMPLIST_ROTATE
  98.     xfmark_T    tempmark;
  99. #endif
  100.  
  101.     /* for :global the mark is set only once */
  102.     if (global_busy)
  103.     return;
  104.  
  105.     curwin->w_prev_pcmark = curwin->w_pcmark;
  106.     curwin->w_pcmark = curwin->w_cursor;
  107.  
  108. #ifdef FEAT_JUMPLIST
  109. # ifdef JUMPLIST_ROTATE
  110.     /*
  111.      * If last used entry is not at the top, put it at the top by rotating
  112.      * the stack until it is (the newer entries will be at the bottom).
  113.      * Keep one entry (the last used one) at the top.
  114.      */
  115.     if (curwin->w_jumplistidx < curwin->w_jumplistlen)
  116.     ++curwin->w_jumplistidx;
  117.     while (curwin->w_jumplistidx < curwin->w_jumplistlen)
  118.     {
  119.     tempmark = curwin->w_jumplist[curwin->w_jumplistlen - 1];
  120.     for (i = curwin->w_jumplistlen - 1; i > 0; --i)
  121.         curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
  122.     curwin->w_jumplist[0] = tempmark;
  123.     ++curwin->w_jumplistidx;
  124.     }
  125. # endif
  126.  
  127.     /* If jumplist is full: remove oldest entry */
  128.     if (++curwin->w_jumplistlen > JUMPLISTSIZE)
  129.     {
  130.     curwin->w_jumplistlen = JUMPLISTSIZE;
  131.     vim_free(curwin->w_jumplist[0].fname);
  132.     for (i = 1; i < JUMPLISTSIZE; ++i)
  133.         curwin->w_jumplist[i - 1] = curwin->w_jumplist[i];
  134.     }
  135.     curwin->w_jumplistidx = curwin->w_jumplistlen;
  136.     fm = &curwin->w_jumplist[curwin->w_jumplistlen - 1];
  137.  
  138.     fm->fmark.mark = curwin->w_pcmark;
  139.     fm->fmark.fnum = curbuf->b_fnum;
  140.     fm->fname = NULL;
  141. #endif
  142. }
  143.  
  144. /*
  145.  * To change context, call setpcmark(), then move the current position to
  146.  * where ever, then call checkpcmark().  This ensures that the previous
  147.  * context will only be changed if the cursor moved to a different line.
  148.  * If pcmark was deleted (with "dG") the previous mark is restored.
  149.  */
  150.     void
  151. checkpcmark()
  152. {
  153.     if (curwin->w_prev_pcmark.lnum != 0
  154.         && (equal(curwin->w_pcmark, curwin->w_cursor)
  155.         || curwin->w_pcmark.lnum == 0))
  156.     {
  157.     curwin->w_pcmark = curwin->w_prev_pcmark;
  158.     curwin->w_prev_pcmark.lnum = 0;        /* Show it has been checked */
  159.     }
  160. }
  161.  
  162. #if defined(FEAT_JUMPLIST) || defined(PROTO)
  163. /*
  164.  * move "count" positions in the jump list (count may be negative)
  165.  */
  166.     pos_T *
  167. movemark(count)
  168.     int count;
  169. {
  170.     pos_T    *pos;
  171.     xfmark_T    *jmp;
  172.  
  173.     cleanup_jumplist();
  174.  
  175.     if (curwin->w_jumplistlen == 0)        /* nothing to jump to */
  176.     return (pos_T *)NULL;
  177.  
  178.     for (;;)
  179.     {
  180.     if (curwin->w_jumplistidx + count < 0
  181.         || curwin->w_jumplistidx + count >= curwin->w_jumplistlen)
  182.         return (pos_T *)NULL;
  183.  
  184.     /*
  185.      * if first CTRL-O or CTRL-I command after a jump, add cursor position
  186.      * to list.  Careful: If there are duplicates (CTRL-O immidiately after
  187.      * starting Vim on a file), another entry may have been removed.
  188.      */
  189.     if (curwin->w_jumplistidx == curwin->w_jumplistlen)
  190.     {
  191.         setpcmark();
  192.         --curwin->w_jumplistidx;    /* skip the new entry */
  193.         if (curwin->w_jumplistidx + count < 0)
  194.         return (pos_T *)NULL;
  195.     }
  196.  
  197.     curwin->w_jumplistidx += count;
  198.  
  199.     jmp = curwin->w_jumplist + curwin->w_jumplistidx;
  200.     if (jmp->fmark.fnum == 0)
  201.         fname2fnum(jmp);
  202.     if (jmp->fmark.fnum != curbuf->b_fnum)
  203.     {
  204.         /* jump to other file */
  205.         if (buflist_findnr(jmp->fmark.fnum) == NULL)
  206.         {                         /* Skip this one .. */
  207.         count += count < 0 ? -1 : 1;
  208.         continue;
  209.         }
  210.         if (buflist_getfile(jmp->fmark.fnum, jmp->fmark.mark.lnum,
  211.                                 0, FALSE) == FAIL)
  212.         return (pos_T *)NULL;
  213.         /* Set lnum again, autocommands my have changed it */
  214.         curwin->w_cursor = jmp->fmark.mark;
  215.         pos = (pos_T *)-1;
  216.     }
  217.     else
  218.         pos = &(jmp->fmark.mark);
  219.     return pos;
  220.     }
  221. }
  222. #endif
  223.  
  224. /*
  225.  * Find mark "c".
  226.  * Returns:
  227.  * - pointer to pos_T if found.  lnum is 0 when mark not set, -1 when mark is
  228.  *   in another file which can't be gotten. (caller needs to check lnum!)
  229.  * - NULL if there is no mark called 'c'.
  230.  * - -1 if mark is in other file and jumped there (only if changefile is TRUE)
  231.  */
  232.     pos_T *
  233. getmark(c, changefile)
  234.     int        c;
  235.     int        changefile;        /* allowed to edit another file */
  236. {
  237.     pos_T        *posp;
  238. #ifdef FEAT_VISUAL
  239.     pos_T        *startp, *endp;
  240. #endif
  241.     static pos_T    pos_copy;
  242.  
  243.     posp = NULL;
  244.  
  245.     /* Check for special key, can't be a mark name and might cause islower()
  246.      * to crash. */
  247.     if (c < 0)
  248.     return posp;
  249. #ifndef EBCDIC
  250.     if (c > '~')            /* check for islower()/isupper() */
  251.     ;
  252.     else
  253. #endif
  254.     if (c == '\'' || c == '`')    /* previous context mark */
  255.     {
  256.     pos_copy = curwin->w_pcmark;    /* need to make a copy because */
  257.     posp = &pos_copy;        /*   w_pcmark may be changed soon */
  258.     }
  259.     else if (c == '"')            /* to pos when leaving buffer */
  260.     posp = &(curbuf->b_last_cursor);
  261.     else if (c == '^')            /* to where Insert mode stopped */
  262.     posp = &(curbuf->b_last_insert);
  263.     else if (c == '.')            /* to where last change was made */
  264.     posp = &(curbuf->b_last_change);
  265.     else if (c == '[')            /* to start of previous operator */
  266.     posp = &(curbuf->b_op_start);
  267.     else if (c == ']')            /* to end of previous operator */
  268.     posp = &(curbuf->b_op_end);
  269.     else if (c == '{' || c == '}')    /* to previous/next paragraph */
  270.     {
  271.     pos_T    pos;
  272.     oparg_T    oa;
  273.  
  274.     pos = curwin->w_cursor;
  275.     if (findpar(&oa, c == '}' ? FORWARD : BACKWARD, 1L, NUL, FALSE))
  276.     {
  277.         pos_copy = curwin->w_cursor;
  278.         posp = &pos_copy;
  279.     }
  280.     curwin->w_cursor = pos;
  281.     }
  282.     else if (c == '(' || c == ')')    /* to previous/next sentence */
  283.     {
  284.     pos_T    pos;
  285.  
  286.     pos = curwin->w_cursor;
  287.     if (findsent(c == ')' ? FORWARD : BACKWARD, 1L))
  288.     {
  289.         pos_copy = curwin->w_cursor;
  290.         posp = &pos_copy;
  291.     }
  292.     curwin->w_cursor = pos;
  293.     }
  294. #ifdef FEAT_VISUAL
  295.     else if (c == '<' || c == '>')    /* start/end of visual area */
  296.     {
  297.     startp = &curbuf->b_visual_start;
  298.     endp = &curbuf->b_visual_end;
  299.     if ((c == '<') == lt(*startp, *endp))
  300.     {
  301.         posp = startp;
  302.     }
  303.     else
  304.     {
  305.         posp = endp;
  306.     }
  307.     /*
  308.      * For Visual line mode, set mark at begin or end of line
  309.      */
  310.     if (curbuf->b_visual_mode == 'V')
  311.     {
  312.         pos_copy = *posp;
  313.         posp = &pos_copy;
  314.         if (c == '<')
  315.         pos_copy.col = 0;
  316.         else
  317.         pos_copy.col = MAXCOL;
  318. #ifdef FEAT_VIRTUALEDIT
  319.         pos_copy.coladd = 0;
  320. #endif
  321.     }
  322.     }
  323. #endif
  324.     else if (ASCII_ISLOWER(c))        /* normal named mark */
  325.     {
  326.     posp = &(curbuf->b_namedm[c - 'a']);
  327.     }
  328.     else if (ASCII_ISUPPER(c) || vim_isdigit(c))    /* named file mark */
  329.     {
  330.     if (vim_isdigit(c))
  331.         c = c - '0' + NMARKS;
  332.     else
  333.         c -= 'A';
  334.     posp = &(namedfm[c].fmark.mark);
  335.  
  336.     if (namedfm[c].fmark.fnum == 0)
  337.         fname2fnum(&namedfm[c]);
  338.     if (namedfm[c].fmark.fnum != curbuf->b_fnum)
  339.     {
  340.         posp = &pos_copy;
  341.  
  342.         /* mark is in another file */
  343.         if (namedfm[c].fmark.mark.lnum != 0
  344.                        && changefile && namedfm[c].fmark.fnum)
  345.         {
  346.         if (buflist_getfile(namedfm[c].fmark.fnum,
  347.                       (linenr_T)1, GETF_SETMARK, FALSE) == OK)
  348.         {
  349.             /* Set the lnum now, autocommands could have changed it */
  350.             curwin->w_cursor = namedfm[c].fmark.mark;
  351.             return (pos_T *)-1;
  352.         }
  353.         pos_copy.lnum = -1;    /* can't get file */
  354.         }
  355.         else
  356.         pos_copy.lnum = 0;    /* mark exists, but is not valid in
  357.                        current buffer */
  358.     }
  359.     }
  360.  
  361.     return posp;
  362. }
  363.  
  364. /*
  365.  * Search for the next named mark in the current file.
  366.  *
  367.  * Returns pointer to pos_T of the next mark or NULL if no mark is found.
  368.  */
  369.     pos_T *
  370. getnextmark(startpos, dir, begin_line)
  371.     pos_T    *startpos;    /* where to start */
  372.     int        dir;    /* direction for search */
  373.     int        begin_line;
  374. {
  375.     int        i;
  376.     pos_T    *result = NULL;
  377.     pos_T    pos;
  378.  
  379.     pos = *startpos;
  380.  
  381.     /* When searching backward and leaving the cursor on the first non-blank,
  382.      * position must be in a previous line.
  383.      * When searching forward and leaving the cursor on the first non-blank,
  384.      * position must be in a next line. */
  385.     if (dir == BACKWARD && begin_line)
  386.     pos.col = 0;
  387.     else if (dir == FORWARD && begin_line)
  388.     pos.col = MAXCOL;
  389.  
  390.     for (i = 0; i < NMARKS; i++)
  391.     {
  392.     if (curbuf->b_namedm[i].lnum > 0)
  393.     {
  394.         if (dir == FORWARD)
  395.         {
  396.         if ((result == NULL || lt(curbuf->b_namedm[i], *result))
  397.             && lt(pos, curbuf->b_namedm[i]))
  398.             result = &curbuf->b_namedm[i];
  399.         }
  400.         else
  401.         {
  402.         if ((result == NULL || lt(*result, curbuf->b_namedm[i]))
  403.             && lt(curbuf->b_namedm[i], pos))
  404.             result = &curbuf->b_namedm[i];
  405.         }
  406.     }
  407.     }
  408.  
  409.     return result;
  410. }
  411.  
  412. /*
  413.  * For an xtended filemark: set the fnum from the fname.
  414.  * This is used for marks obtained from the .viminfo file.  It's postponed
  415.  * until the mark is used to avoid a long startup delay.
  416.  */
  417.     static void
  418. fname2fnum(fm)
  419.     xfmark_T    *fm;
  420. {
  421.     char_u    *p;
  422.  
  423.     if (fm->fname != NULL)
  424.     {
  425.     /*
  426.      * First expand "~/" in the file name to the home directory.
  427.      * Try to shorten the file name.
  428.      */
  429.     expand_env(fm->fname, NameBuff, MAXPATHL);
  430.     mch_dirname(IObuff, IOSIZE);
  431.     p = shorten_fname(NameBuff, IObuff);
  432.  
  433.     /* buflist_new() will call fmarks_check_names() */
  434.     (void)buflist_new(NameBuff, p, (linenr_T)1, 0);
  435.     }
  436. }
  437.  
  438. /*
  439.  * Check all file marks for a name that matches the file name in buf.
  440.  * May replace the name with an fnum.
  441.  * Used for marks that come from the .viminfo file.
  442.  */
  443.     void
  444. fmarks_check_names(buf)
  445.     buf_T    *buf;
  446. {
  447.     char_u    *name;
  448.     int        i;
  449. #ifdef FEAT_JUMPLIST
  450.     win_T    *wp;
  451. #endif
  452.  
  453.     if (buf->b_ffname == NULL)
  454.     return;
  455.  
  456.     name = home_replace_save(buf, buf->b_ffname);
  457.     if (name == NULL)
  458.     return;
  459.  
  460.     for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
  461.     fmarks_check_one(&namedfm[i], name, buf);
  462.  
  463. #ifdef FEAT_JUMPLIST
  464.     FOR_ALL_WINDOWS(wp)
  465.     {
  466.     for (i = 0; i < wp->w_jumplistlen; ++i)
  467.         fmarks_check_one(&wp->w_jumplist[i], name, buf);
  468.     }
  469. #endif
  470.  
  471.     vim_free(name);
  472. }
  473.  
  474.     static void
  475. fmarks_check_one(fm, name, buf)
  476.     xfmark_T    *fm;
  477.     char_u    *name;
  478.     buf_T    *buf;
  479. {
  480.     if (fm->fmark.fnum == 0
  481.         && fm->fname != NULL
  482.         && fnamecmp(name, fm->fname) == 0)
  483.     {
  484.     fm->fmark.fnum = buf->b_fnum;
  485.     vim_free(fm->fname);
  486.     fm->fname = NULL;
  487.     }
  488. }
  489.  
  490. /*
  491.  * Check a if a position from a mark is valid.
  492.  * Give and error message and return FAIL if not.
  493.  */
  494.     int
  495. check_mark(pos)
  496.     pos_T    *pos;
  497. {
  498.     if (pos == NULL)
  499.     {
  500.     EMSG(_(e_umark));
  501.     return FAIL;
  502.     }
  503.     if (pos->lnum <= 0)
  504.     {
  505.     /* lnum is negative if mark is in another file can can't get that
  506.      * file, error message already give then. */
  507.     if (pos->lnum == 0)
  508.         EMSG(_(e_marknotset));
  509.     return FAIL;
  510.     }
  511.     if (pos->lnum > curbuf->b_ml.ml_line_count)
  512.     {
  513.     EMSG(_(e_markinval));
  514.     return FAIL;
  515.     }
  516.     return OK;
  517. }
  518.  
  519. /*
  520.  * clrallmarks() - clear all marks in the buffer 'buf'
  521.  *
  522.  * Used mainly when trashing the entire buffer during ":e" type commands
  523.  */
  524.     void
  525. clrallmarks(buf)
  526.     buf_T    *buf;
  527. {
  528.     static int        i = -1;
  529.  
  530.     if (i == -1)    /* first call ever: initialize */
  531.     for (i = 0; i < NMARKS + 1; i++)
  532.     {
  533.         namedfm[i].fmark.mark.lnum = 0;
  534.         namedfm[i].fname = NULL;
  535.     }
  536.  
  537.     for (i = 0; i < NMARKS; i++)
  538.     buf->b_namedm[i].lnum = 0;
  539.     buf->b_op_start.lnum = 0;        /* start/end op mark cleared */
  540.     buf->b_op_end.lnum = 0;
  541.     buf->b_last_cursor.lnum = 1;    /* '" mark cleared */
  542.     buf->b_last_cursor.col = 0;
  543. #ifdef FEAT_VIRTUALEDIT
  544.     buf->b_last_cursor.coladd = 0;
  545. #endif
  546.     buf->b_last_insert.lnum = 0;    /* '^ mark cleared */
  547.     buf->b_last_change.lnum = 0;    /* '. mark cleared */
  548. }
  549.  
  550. /*
  551.  * Get name of file from a filemark.
  552.  * When it's in the current buffer, return the text at the mark.
  553.  * Returns an allocated string.
  554.  */
  555.     char_u *
  556. fm_getname(fmark, lead_len)
  557.     fmark_T    *fmark;
  558.     int        lead_len;
  559. {
  560.     if (fmark->fnum == curbuf->b_fnum)            /* current buffer */
  561.     return mark_line(&(fmark->mark), lead_len);
  562.     return buflist_nr2name(fmark->fnum, FALSE, TRUE);
  563. }
  564.  
  565. /*
  566.  * Return the line at mark "mp".  Truncate to fit in window.
  567.  * The returned string has been allocated.
  568.  */
  569.     static char_u *
  570. mark_line(mp, lead_len)
  571.     pos_T    *mp;
  572.     int        lead_len;
  573. {
  574.     char_u    *s, *p;
  575.     int        len;
  576.  
  577.     if (mp->lnum == 0 || mp->lnum > curbuf->b_ml.ml_line_count)
  578.     return vim_strsave((char_u *)"-invalid-");
  579.     s = vim_strnsave(skipwhite(ml_get(mp->lnum)), (int)Columns);
  580.     if (s == NULL)
  581.     return NULL;
  582.     /* Truncate the line to fit it in the window */
  583.     len = 0;
  584.     for (p = s; *p != NUL; ++p)
  585.     {
  586.     len += ptr2cells(p);
  587.     if (len >= Columns - lead_len)
  588.         break;
  589. #ifdef FEAT_MBYTE
  590.     if (has_mbyte)
  591.         p += (*mb_ptr2len_check)(p) - 1;
  592. #endif
  593.     }
  594.     *p = NUL;
  595.     return s;
  596. }
  597.  
  598. /*
  599.  * print the marks
  600.  */
  601.     void
  602. do_marks(eap)
  603.     exarg_T    *eap;
  604. {
  605.     char_u    *arg = eap->arg;
  606.     int        i;
  607.     char_u    *name;
  608.  
  609.     if (arg != NULL && *arg == NUL)
  610.     arg = NULL;
  611.  
  612.     show_one_mark('\'', arg, &curwin->w_pcmark, NULL, TRUE);
  613.     for (i = 0; i < NMARKS; ++i)
  614.     show_one_mark(i + 'a', arg, &curbuf->b_namedm[i], NULL, TRUE);
  615.     for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
  616.     {
  617.     if (namedfm[i].fmark.fnum != 0)
  618.         name = fm_getname(&namedfm[i].fmark, 15);
  619.     else
  620.         name = namedfm[i].fname;
  621.     if (name != NULL)
  622.     {
  623.         show_one_mark(i >= NMARKS ? i - NMARKS + '0' : i + 'A',
  624.             arg, &namedfm[i].fmark.mark, name,
  625.             namedfm[i].fmark.fnum == curbuf->b_fnum);
  626.         if (namedfm[i].fmark.fnum != 0)
  627.         vim_free(name);
  628.     }
  629.     }
  630.     show_one_mark('"', arg, &curbuf->b_last_cursor, NULL, TRUE);
  631.     show_one_mark('[', arg, &curbuf->b_op_start, NULL, TRUE);
  632.     show_one_mark(']', arg, &curbuf->b_op_end, NULL, TRUE);
  633.     show_one_mark('^', arg, &curbuf->b_last_insert, NULL, TRUE);
  634.     show_one_mark('.', arg, &curbuf->b_last_change, NULL, TRUE);
  635. #ifdef FEAT_VISUAL
  636.     show_one_mark('<', arg, &curbuf->b_visual_start, NULL, TRUE);
  637.     show_one_mark('>', arg, &curbuf->b_visual_end, NULL, TRUE);
  638. #endif
  639.     show_one_mark(-1, arg, NULL, NULL, FALSE);
  640. }
  641.  
  642.     static void
  643. show_one_mark(c, arg, p, name, current)
  644.     int        c;
  645.     char_u    *arg;
  646.     pos_T    *p;
  647.     char_u    *name;
  648.     int        current;    /* in current file */
  649. {
  650.     static int    did_title = FALSE;
  651.     int        mustfree = FALSE;
  652.  
  653.     if (c == -1)                /* finish up */
  654.     {
  655.     if (did_title)
  656.         did_title = FALSE;
  657.     else
  658.     {
  659.         if (arg == NULL)
  660.         MSG(_("No marks set"));
  661.         else
  662.         EMSG2(_("E283: No marks matching \"%s\""), arg);
  663.     }
  664.     }
  665.     /* don't output anything if 'q' typed at --more-- prompt */
  666.     else if (!got_int
  667.         && (arg == NULL || vim_strchr(arg, c) != NULL)
  668.         && p->lnum != 0)
  669.     {
  670.     if (!did_title)
  671.     {
  672.         /* Highlight title */
  673.         MSG_PUTS_TITLE(_("\nmark line  col file/text"));
  674.         did_title = TRUE;
  675.     }
  676.     msg_putchar('\n');
  677.     if (!got_int)
  678.     {
  679.         sprintf((char *)IObuff, " %c %6ld %4d ", c, p->lnum, p->col);
  680.         msg_outtrans(IObuff);
  681.         if (name == NULL && current)
  682.         {
  683.         name = mark_line(p, 15);
  684.         mustfree = TRUE;
  685.         }
  686.         if (name != NULL)
  687.         {
  688.         msg_outtrans_attr(name, current ? hl_attr(HLF_D) : 0);
  689.         if (mustfree)
  690.             vim_free(name);
  691.         }
  692.     }
  693.     out_flush();            /* show one line at a time */
  694.     }
  695. }
  696.  
  697. #if defined(FEAT_JUMPLIST) || defined(PROTO)
  698. /*
  699.  * print the jumplist
  700.  */
  701. /*ARGSUSED*/
  702.     void
  703. ex_jumps(eap)
  704.     exarg_T    *eap;
  705. {
  706.     int        i;
  707.     char_u    *name;
  708.  
  709.     cleanup_jumplist();
  710.     /* Highlight title */
  711.     MSG_PUTS_TITLE(_("\n jump line  col file/text"));
  712.     for (i = 0; i < curwin->w_jumplistlen; ++i)
  713.     {
  714.     if (curwin->w_jumplist[i].fmark.mark.lnum != 0)
  715.     {
  716.         if (curwin->w_jumplist[i].fmark.fnum == 0)
  717.         fname2fnum(&curwin->w_jumplist[i]);
  718.         name = fm_getname(&curwin->w_jumplist[i].fmark, 16);
  719.         if (name == NULL)        /* file name not available */
  720.         continue;
  721.  
  722.         msg_putchar('\n');
  723.         sprintf((char *)IObuff, "%c %2d %5ld %4d ",
  724.         i == curwin->w_jumplistidx ? '>' : ' ',
  725.         i > curwin->w_jumplistidx ? i - curwin->w_jumplistidx
  726.                       : curwin->w_jumplistidx - i,
  727.         curwin->w_jumplist[i].fmark.mark.lnum,
  728.         curwin->w_jumplist[i].fmark.mark.col);
  729.         msg_outtrans(IObuff);
  730.         msg_outtrans_attr(name,
  731.                 curwin->w_jumplist[i].fmark.fnum == curbuf->b_fnum
  732.                             ? hl_attr(HLF_D) : 0);
  733.         vim_free(name);
  734.     }
  735.     out_flush();
  736.     }
  737.     if (curwin->w_jumplistidx == curwin->w_jumplistlen)
  738.     MSG_PUTS("\n>");
  739. }
  740. #endif
  741.  
  742. #define one_adjust(add) \
  743.     { \
  744.     lp = add; \
  745.     if (*lp >= line1 && *lp <= line2) \
  746.     { \
  747.         if (amount == MAXLNUM) \
  748.         *lp = 0; \
  749.         else \
  750.         *lp += amount; \
  751.     } \
  752.     else if (amount_after && *lp > line2) \
  753.         *lp += amount_after; \
  754.     }
  755.  
  756. /* don't delete the line, just put at first deleted line */
  757. #define one_adjust_nodel(add) \
  758.     { \
  759.     lp = add; \
  760.     if (*lp >= line1 && *lp <= line2) \
  761.     { \
  762.         if (amount == MAXLNUM) \
  763.         *lp = line1; \
  764.         else \
  765.         *lp += amount; \
  766.     } \
  767.     else if (amount_after && *lp > line2) \
  768.         *lp += amount_after; \
  769.     }
  770.  
  771. /*
  772.  * Adjust marks between line1 and line2 (inclusive) to move 'amount' lines.
  773.  * Must be called before changed_*(), appended_lines() or deleted_lines().
  774.  * May be called before or after changing the text.
  775.  * When deleting lines line1 to line2, use an 'amount' of MAXLNUM: The marks
  776.  * within this range are made invalid.
  777.  * If 'amount_after' is non-zero adjust marks after line2.
  778.  * Example: Delete lines 34 and 35: mark_adjust(34, 35, MAXLNUM, -2);
  779.  * Example: Insert two lines below 55: mark_adjust(56, MAXLNUM, 2, 0);
  780.  *                   or: mark_adjust(56, 55, MAXLNUM, 2);
  781.  */
  782.     void
  783. mark_adjust(line1, line2, amount, amount_after)
  784.     linenr_T    line1;
  785.     linenr_T    line2;
  786.     long    amount;
  787.     long    amount_after;
  788. {
  789.     int        i;
  790.     int        fnum = curbuf->b_fnum;
  791.     linenr_T    *lp;
  792.     win_T    *win;
  793.  
  794.     if (line2 < line1 && amount_after == 0L)        /* nothing to do */
  795.     return;
  796.  
  797.     /* named marks, lower case and upper case */
  798.     for (i = 0; i < NMARKS; i++)
  799.     {
  800.     one_adjust(&(curbuf->b_namedm[i].lnum));
  801.     if (namedfm[i].fmark.fnum == fnum)
  802.         one_adjust(&(namedfm[i].fmark.mark.lnum));
  803.     }
  804.     for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
  805.     {
  806.     if (namedfm[i].fmark.fnum == fnum)
  807.         one_adjust(&(namedfm[i].fmark.mark.lnum));
  808.     }
  809.  
  810.     /* previous context mark */
  811.     one_adjust(&(curwin->w_pcmark.lnum));
  812.  
  813.     /* previous pcmark */
  814.     one_adjust(&(curwin->w_prev_pcmark.lnum));
  815.  
  816.     /* last Insert position */
  817.     one_adjust(&(curbuf->b_last_insert.lnum));
  818.  
  819.     /* last change position */
  820.     one_adjust(&(curbuf->b_last_change.lnum));
  821.  
  822. #ifdef FEAT_VISUAL
  823.     /* Visual area */
  824.     one_adjust_nodel(&(curbuf->b_visual_start.lnum));
  825.     one_adjust_nodel(&(curbuf->b_visual_end.lnum));
  826. #endif
  827.  
  828. #ifdef FEAT_QUICKFIX
  829.     /* quickfix marks */
  830.     qf_mark_adjust(line1, line2, amount, amount_after);
  831. #endif
  832.  
  833.     /*
  834.      * Adjust items in all windows related to the current buffer.
  835.      */
  836.     FOR_ALL_WINDOWS(win)
  837.     {
  838. #ifdef FEAT_JUMPLIST
  839.     /* Marks in the jumplist.  When deleting lines, this may create
  840.      * duplicate marks in the jumplist, they will be removed later. */
  841.     for (i = 0; i < win->w_jumplistlen; ++i)
  842.         if (win->w_jumplist[i].fmark.fnum == fnum)
  843.         one_adjust_nodel(&(win->w_jumplist[i].fmark.mark.lnum));
  844. #endif
  845.  
  846.     if (win->w_buffer == curbuf)
  847.     {
  848.         /* marks in the tag stack */
  849.         for (i = 0; i < win->w_tagstacklen; i++)
  850.         if (win->w_tagstack[i].fmark.fnum == fnum)
  851.             one_adjust_nodel(&(win->w_tagstack[i].fmark.mark.lnum));
  852.  
  853. #ifdef FEAT_VISUAL
  854.         /* the displayed Visual area */
  855.         if (win->w_old_cursor_lnum != 0)
  856.         {
  857.         one_adjust_nodel(&(win->w_old_cursor_lnum));
  858.         one_adjust_nodel(&(win->w_old_visual_lnum));
  859.         }
  860. #endif
  861.  
  862.         /* topline and cursor position for windows with the same buffer
  863.          * other than the current window */
  864.         if (win != curwin)
  865.         {
  866.         if (win->w_topline >= line1 && win->w_topline <= line2)
  867.         {
  868.             if (amount == MAXLNUM)        /* topline is deleted */
  869.             {
  870.             if (line1 <= 1)
  871.                 win->w_topline = 1;
  872.             else
  873.                 win->w_topline = line1 - 1;
  874.             }
  875.             else        /* keep topline on the same line */
  876.             win->w_topline += amount;
  877. #ifdef FEAT_DIFF
  878.             win->w_topfill = 0;
  879. #endif
  880.         }
  881.         else if (amount_after && win->w_topline > line2)
  882.         {
  883.             win->w_topline += amount_after;
  884. #ifdef FEAT_DIFF
  885.             win->w_topfill = 0;
  886. #endif
  887.         }
  888.         if (win->w_cursor.lnum >= line1 && win->w_cursor.lnum <= line2)
  889.         {
  890.             if (amount == MAXLNUM) /* line with cursor is deleted */
  891.             {
  892.             if (line1 <= 1)
  893.                 win->w_cursor.lnum = 1;
  894.             else
  895.                 win->w_cursor.lnum = line1 - 1;
  896.             win->w_cursor.col = 0;
  897.             }
  898.             else        /* keep cursor on the same line */
  899.             win->w_cursor.lnum += amount;
  900.         }
  901.         else if (amount_after && win->w_cursor.lnum > line2)
  902.             win->w_cursor.lnum += amount_after;
  903.         }
  904.  
  905. #ifdef FEAT_FOLDING
  906.         /* adjust folds */
  907.         foldMarkAdjust(win, line1, line2, amount, amount_after);
  908. #endif
  909.     }
  910.     }
  911.  
  912. #ifdef FEAT_DIFF
  913.     /* adjust diffs */
  914.     diff_mark_adjust(line1, line2, amount, amount_after);
  915. #endif
  916.  
  917. #ifdef FEAT_SIGNS
  918.     sign_mark_adjust(line1, line2, amount, amount_after);
  919. #endif
  920. }
  921.  
  922. #ifdef FEAT_JUMPLIST
  923. /*
  924.  * When deleting lines, this may create duplicate marks in the
  925.  * jumplist. They will be removed here for the current window.
  926.  */
  927.     static void
  928. cleanup_jumplist()
  929. {
  930.     int        i;
  931.     int        from, to;
  932.  
  933.     to = 0;
  934.     for (from = 0; from < curwin->w_jumplistlen; ++from)
  935.     {
  936.     if (curwin->w_jumplistidx == from)
  937.         curwin->w_jumplistidx = to;
  938.     for (i = from + 1; i < curwin->w_jumplistlen; ++i)
  939.         if (curwin->w_jumplist[i].fmark.fnum
  940.                     == curwin->w_jumplist[from].fmark.fnum
  941.             && curwin->w_jumplist[from].fmark.fnum != 0
  942.             && curwin->w_jumplist[i].fmark.mark.lnum
  943.                   == curwin->w_jumplist[from].fmark.mark.lnum)
  944.         break;
  945.     if (i >= curwin->w_jumplistlen)        /* no duplicate */
  946.         curwin->w_jumplist[to++] = curwin->w_jumplist[from];
  947.     else
  948.         vim_free(curwin->w_jumplist[from].fname);
  949.     }
  950.     if (curwin->w_jumplistidx == curwin->w_jumplistlen)
  951.     curwin->w_jumplistidx = to;
  952.     curwin->w_jumplistlen = to;
  953. }
  954.  
  955. # if defined(FEAT_WINDOWS) || defined(PROTO)
  956. /*
  957.  * Copy the jumplist from window "from" to window "to".
  958.  */
  959.     void
  960. copy_jumplist(from, to)
  961.     win_T    *from;
  962.     win_T    *to;
  963. {
  964.     int        i;
  965.  
  966.     for (i = 0; i < from->w_jumplistlen; ++i)
  967.     {
  968.     to->w_jumplist[i] = from->w_jumplist[i];
  969.     if (from->w_jumplist[i].fname != NULL)
  970.         to->w_jumplist[i].fname = vim_strsave(from->w_jumplist[i].fname);
  971.     }
  972.     to->w_jumplistlen = from->w_jumplistlen;
  973.     to->w_jumplistidx = from->w_jumplistidx;
  974. }
  975.  
  976. /*
  977.  * Free items in the jumplist of window "wp".
  978.  */
  979.     void
  980. free_jumplist(wp)
  981.     win_T    *wp;
  982. {
  983.     int        i;
  984.  
  985.     for (i = 0; i < wp->w_jumplistlen; ++i)
  986.     vim_free(wp->w_jumplist[i].fname);
  987. }
  988. # endif
  989. #endif /* FEAT_JUMPLIST */
  990.  
  991.     void
  992. set_last_cursor(win)
  993.     win_T    *win;
  994. {
  995.     win->w_buffer->b_last_cursor = win->w_cursor;
  996. }
  997.  
  998. #if defined(FEAT_VIMINFO) || defined(PROTO)
  999.     int
  1000. read_viminfo_filemark(virp, force)
  1001.     vir_T    *virp;
  1002.     int        force;
  1003. {
  1004.     char_u    *str;
  1005.     xfmark_T    *fm;
  1006.     int        i;
  1007.  
  1008.     /* We only get here if line[0] == '\'' or '-'.
  1009.      * Illegal mark names are ignored (for future expansion). */
  1010.     str = virp->vir_line + 1;
  1011.     if (
  1012. #ifndef EBCDIC
  1013.         *str <= 127 &&
  1014. #endif
  1015.         ((*virp->vir_line == '\'' && (isdigit(*str) || isupper(*str)))
  1016.          || (*virp->vir_line == '-' && *str == '\'')))
  1017.     {
  1018.     if (*str == '\'')
  1019.     {
  1020. #ifdef FEAT_JUMPLIST
  1021.         /* If the jumplist isn't full insert fmark as oldest entry */
  1022.         if (curwin->w_jumplistlen == JUMPLISTSIZE)
  1023.         fm = NULL;
  1024.         else
  1025.         {
  1026.         for (i = curwin->w_jumplistlen; i > 0; --i)
  1027.             curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
  1028.         ++curwin->w_jumplistidx;
  1029.         ++curwin->w_jumplistlen;
  1030.         fm = &curwin->w_jumplist[0];
  1031.         fm->fmark.mark.lnum = 0;
  1032.         fm->fname = NULL;
  1033.         }
  1034. #else
  1035.         fm = NULL;
  1036. #endif
  1037.     }
  1038.     else if (isdigit(*str))
  1039.         fm = &namedfm[*str - '0' + NMARKS];
  1040.     else
  1041.         fm = &namedfm[*str - 'A'];
  1042.     if (fm != NULL && (fm->fmark.mark.lnum == 0 || force))
  1043.     {
  1044.         str = skipwhite(str + 1);
  1045.         fm->fmark.mark.lnum = getdigits(&str);
  1046.         str = skipwhite(str);
  1047.         fm->fmark.mark.col = getdigits(&str);
  1048. #ifdef FEAT_VIRTUALEDIT
  1049.         fm->fmark.mark.coladd = 0;
  1050. #endif
  1051.         fm->fmark.fnum = 0;
  1052.         str = skipwhite(str);
  1053.         vim_free(fm->fname);
  1054.         fm->fname = viminfo_readstring(virp, (int)(str - virp->vir_line),
  1055.                                        FALSE);
  1056.     }
  1057.     }
  1058.     return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
  1059. }
  1060.  
  1061.     void
  1062. write_viminfo_filemarks(fp)
  1063.     FILE    *fp;
  1064. {
  1065.     int        i;
  1066.     char_u    *name;
  1067.     buf_T    *buf;
  1068.     xfmark_T    *fm;
  1069.  
  1070.     if (get_viminfo_parameter('\'') == 0)
  1071.     return;
  1072.  
  1073.     fprintf(fp, _("\n# File marks:\n"));
  1074.  
  1075.     /*
  1076.      * Find a mark that is the same file and position as the cursor.
  1077.      * That one, or else the last one is deleted.
  1078.      * Move '0 to '1, '1 to '2, etc. until the matching one or '9
  1079.      * Set '0 mark to current cursor position.
  1080.      */
  1081.     if (curbuf->b_ffname != NULL && !removable(curbuf->b_ffname))
  1082.     {
  1083.     name = buflist_nr2name(curbuf->b_fnum, TRUE, FALSE);
  1084.     for (i = NMARKS; i < NMARKS + EXTRA_MARKS - 1; ++i)
  1085.         if (namedfm[i].fmark.mark.lnum == curwin->w_cursor.lnum
  1086.             && (namedfm[i].fname == NULL
  1087.                 ? namedfm[i].fmark.fnum == curbuf->b_fnum
  1088.                 : (name != NULL
  1089.                     && STRCMP(name, namedfm[i].fname) == 0)))
  1090.         break;
  1091.     vim_free(name);
  1092.  
  1093.     vim_free(namedfm[i].fname);
  1094.     for ( ; i > NMARKS; --i)
  1095.         namedfm[i] = namedfm[i - 1];
  1096.     namedfm[NMARKS].fmark.mark = curwin->w_cursor;
  1097.     namedfm[NMARKS].fmark.fnum = curbuf->b_fnum;
  1098.     namedfm[NMARKS].fname = NULL;
  1099.     }
  1100.  
  1101.     /* Write the filemarks '0 - '9 and 'A - 'Z */
  1102.     for (i = 0; i < NMARKS + EXTRA_MARKS; i++)
  1103.     write_one_filemark(fp, &namedfm[i], '\'',
  1104.                      i < NMARKS ? i + 'A' : i - NMARKS + '0');
  1105.  
  1106. #ifdef FEAT_JUMPLIST
  1107.     /* Write the jumplist with -' */
  1108.     fprintf(fp, _("\n# Jumplist (newest first):\n"));
  1109.     setpcmark();    /* add current cursor position */
  1110.     cleanup_jumplist();
  1111.     for (fm = &curwin->w_jumplist[curwin->w_jumplistlen - 1];
  1112.                        fm >= &curwin->w_jumplist[0]; --fm)
  1113.     {
  1114.     if (fm->fmark.fnum == 0
  1115.         || ((buf = buflist_findnr(fm->fmark.fnum)) != NULL
  1116.             && !removable(buf->b_ffname)))
  1117.         write_one_filemark(fp, fm, '-', '\'');
  1118.     }
  1119. #endif
  1120. }
  1121.  
  1122.     static void
  1123. write_one_filemark(fp, fm, c1, c2)
  1124.     FILE    *fp;
  1125.     xfmark_T    *fm;
  1126.     int        c1;
  1127.     int        c2;
  1128. {
  1129.     char_u    *name;
  1130.  
  1131.     if (fm->fmark.mark.lnum == 0)    /* not set */
  1132.     return;
  1133.  
  1134.     if (fm->fmark.fnum != 0)        /* there is a buffer */
  1135.     name = buflist_nr2name(fm->fmark.fnum, TRUE, FALSE);
  1136.     else
  1137.     name = fm->fname;        /* use name from .viminfo */
  1138.     if (name == NULL)
  1139.     return;
  1140.  
  1141.     fprintf(fp, "%c%c  %ld  %ld  ", c1, c2, (long)fm->fmark.mark.lnum,
  1142.                             (long)fm->fmark.mark.col);
  1143.     viminfo_writestring(fp, name);
  1144.     if (fm->fmark.fnum != 0)
  1145.     vim_free(name);
  1146. }
  1147.  
  1148. /*
  1149.  * Return TRUE if "name" is on removable media (depending on 'viminfo').
  1150.  */
  1151.     int
  1152. removable(name)
  1153.     char_u  *name;
  1154. {
  1155.     char_u  *p;
  1156.     char_u  part[51];
  1157.     int        retval = FALSE;
  1158.  
  1159.     name = home_replace_save(NULL, name);
  1160.     if (name != NULL)
  1161.     {
  1162.     for (p = p_viminfo; *p; )
  1163.     {
  1164.         copy_option_part(&p, part, 51, ", ");
  1165.         if (part[0] == 'r'
  1166.             && MB_STRNICMP(part + 1, name, STRLEN(part + 1)) == 0)
  1167.         {
  1168.         retval = TRUE;
  1169.         break;
  1170.         }
  1171.     }
  1172.     vim_free(name);
  1173.     }
  1174.     return retval;
  1175. }
  1176.  
  1177. static void write_one_mark __ARGS((FILE *fp_out, int c, pos_T *pos));
  1178.  
  1179. /*
  1180.  * Write all the named marks for all buffers.
  1181.  * Return the number of buffers for which marks have been written.
  1182.  */
  1183.     int
  1184. write_viminfo_marks(fp_out)
  1185.     FILE    *fp_out;
  1186. {
  1187.     int        count;
  1188.     buf_T    *buf;
  1189.     int        is_mark_set;
  1190.     int        i;
  1191. #ifdef FEAT_WINDOWS
  1192.     win_T    *win;
  1193.  
  1194.     /*
  1195.      * Set b_last_cursor for the all buffers that have a window.
  1196.      */
  1197.     for (win = firstwin; win != NULL; win = win->w_next)
  1198.     set_last_cursor(win);
  1199. #else
  1200.     set_last_cursor(curwin);
  1201. #endif
  1202.  
  1203.     fprintf(fp_out, _("\n# History of marks within files (newest to oldest):\n"));
  1204.     count = 0;
  1205.     for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  1206.     {
  1207.     /*
  1208.      * Only write something if buffer has been loaded and at least one
  1209.      * mark is set.
  1210.      */
  1211.     if (buf->b_marks_read)
  1212.     {
  1213.         if (buf->b_last_cursor.lnum != 0)
  1214.         is_mark_set = TRUE;
  1215.         else
  1216.         {
  1217.         is_mark_set = FALSE;
  1218.         for (i = 0; i < NMARKS; i++)
  1219.             if (buf->b_namedm[i].lnum != 0)
  1220.             {
  1221.             is_mark_set = TRUE;
  1222.             break;
  1223.             }
  1224.         }
  1225.         if (is_mark_set && buf->b_ffname != NULL
  1226.               && buf->b_ffname[0] != NUL && !removable(buf->b_ffname))
  1227.         {
  1228.         home_replace(NULL, buf->b_ffname, IObuff, IOSIZE, TRUE);
  1229.         fprintf(fp_out, "\n> ");
  1230.         viminfo_writestring(fp_out, IObuff);
  1231.         write_one_mark(fp_out, '"', &buf->b_last_cursor);
  1232.         write_one_mark(fp_out, '^', &buf->b_last_insert);
  1233.         write_one_mark(fp_out, '.', &buf->b_last_change);
  1234.         for (i = 0; i < NMARKS; i++)
  1235.             write_one_mark(fp_out, 'a' + i, &buf->b_namedm[i]);
  1236.         count++;
  1237.         }
  1238.     }
  1239.     }
  1240.  
  1241.     return count;
  1242. }
  1243.  
  1244.     static void
  1245. write_one_mark(fp_out, c, pos)
  1246.     FILE    *fp_out;
  1247.     int        c;
  1248.     pos_T    *pos;
  1249. {
  1250.     if (pos->lnum != 0)
  1251.     fprintf(fp_out, "\t%c\t%ld\t%d\n", c, (long)pos->lnum, (int)pos->col);
  1252. }
  1253.  
  1254. /*
  1255.  * Handle marks in the viminfo file:
  1256.  * fp_out == NULL   read marks for current buffer only
  1257.  * fp_out != NULL   copy marks for buffers not in buffer list
  1258.  */
  1259.     void
  1260. copy_viminfo_marks(virp, fp_out, count, eof)
  1261.     vir_T    *virp;
  1262.     FILE    *fp_out;
  1263.     int        count;
  1264.     int        eof;
  1265. {
  1266.     char_u    *line = virp->vir_line;
  1267.     buf_T    *buf;
  1268.     int        num_marked_files;
  1269.     int        load_marks;
  1270.     int        copy_marks_out;
  1271.     char_u    *str;
  1272.     int        i;
  1273.     char_u    *p;
  1274.     char_u    *name_buf;
  1275.     pos_T    pos;
  1276.  
  1277.     if ((name_buf = alloc(LSIZE)) == NULL)
  1278.     return;
  1279.     num_marked_files = get_viminfo_parameter('\'');
  1280.     while (!eof && (count < num_marked_files || fp_out == NULL))
  1281.     {
  1282.     if (line[0] != '>')
  1283.     {
  1284.         if (line[0] != '\n' && line[0] != '\r' && line[0] != '#')
  1285.         {
  1286.         if (viminfo_error(_("Missing '>'"), line))
  1287.             break;    /* too many errors, return now */
  1288.         }
  1289.         eof = vim_fgets(line, LSIZE, virp->vir_fd);
  1290.         continue;        /* Skip this dud line */
  1291.     }
  1292.  
  1293.     /*
  1294.      * Handle long line and translate escaped characters.
  1295.      * Find file name, set str to start.
  1296.      * Ignore leading and trailing white space.
  1297.      */
  1298.     str = skipwhite(line + 1);
  1299.     str = viminfo_readstring(virp, (int)(str - virp->vir_line), FALSE);
  1300.     if (str == NULL)
  1301.         continue;
  1302.     p = str + STRLEN(str);
  1303.     while (p != str && (*p == NUL || vim_isspace(*p)))
  1304.         p--;
  1305.     if (*p)
  1306.         p++;
  1307.     *p = NUL;
  1308.  
  1309.     /*
  1310.      * If fp_out == NULL, load marks for current buffer.
  1311.      * If fp_out != NULL, copy marks for buffers not in buflist.
  1312.      */
  1313.     load_marks = copy_marks_out = FALSE;
  1314.     if (fp_out == NULL)
  1315.     {
  1316.         if (curbuf->b_ffname != NULL)
  1317.         {
  1318.         home_replace(NULL, curbuf->b_ffname, name_buf, LSIZE, TRUE);
  1319.         if (fnamecmp(str, name_buf) == 0)
  1320.             load_marks = TRUE;
  1321.         }
  1322.     }
  1323.     else /* fp_out != NULL */
  1324.     {
  1325.         /* This is slow if there are many buffers!! */
  1326.         for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  1327.         if (buf->b_ffname != NULL)
  1328.         {
  1329.             home_replace(NULL, buf->b_ffname, name_buf, LSIZE, TRUE);
  1330.             if (fnamecmp(str, name_buf) == 0)
  1331.             break;
  1332.         }
  1333.  
  1334.         /*
  1335.          * copy marks if the buffer has not been loaded
  1336.          */
  1337.         if (buf == NULL || !buf->b_marks_read)
  1338.         {
  1339.         copy_marks_out = TRUE;
  1340.         fputs("\n> ", fp_out);
  1341.         viminfo_writestring(fp_out, str);
  1342.         count++;
  1343.         }
  1344.     }
  1345.     vim_free(str);
  1346.  
  1347. #ifdef FEAT_VIRTUALEDIT
  1348.     pos.coladd = 0;
  1349. #endif
  1350.     while (!(eof = viminfo_readline(virp)) && line[0] == TAB)
  1351.     {
  1352.         if (load_marks)
  1353.         {
  1354.         if (line[1] != NUL)
  1355.         {
  1356.             sscanf((char *)line + 2, "%ld %d", &pos.lnum, &pos.col);
  1357.             switch (line[1])
  1358.             {
  1359.             case '"': curbuf->b_last_cursor = pos; break;
  1360.             case '^': curbuf->b_last_insert = pos; break;
  1361.             case '.': curbuf->b_last_change = pos; break;
  1362.             default:  if ((i = line[1] - 'a') >= 0 && i < NMARKS)
  1363.                       curbuf->b_namedm[i] = pos;
  1364.             }
  1365.         }
  1366.         }
  1367.         else if (copy_marks_out)
  1368.         fputs((char *)line, fp_out);
  1369.     }
  1370.     if (load_marks)
  1371.         break;
  1372.     }
  1373.     vim_free(name_buf);
  1374. }
  1375. #endif /* FEAT_VIMINFO */
  1376.